package com.cleaningcloud.cld.controller.mini;

import com.cleaningcloud.cld.domain.CldMaterialDetails;
import com.cleaningcloud.cld.domain.CldProjectMaterial;
import com.cleaningcloud.cld.domain.dto.CldMaterialAllocateDto;
import com.cleaningcloud.cld.domain.dto.CldMaterialAppDto;
import com.cleaningcloud.cld.domain.dto.CldMaterialConsumeDto;
import com.cleaningcloud.cld.domain.vo.CldMaterialAllocateVo;
import com.cleaningcloud.cld.domain.vo.CldMaterialAppVo;
import com.cleaningcloud.cld.service.ICldMaterialAllocateService;
import com.cleaningcloud.cld.service.ICldMaterialAppService;
import com.cleaningcloud.cld.service.ICldProjectMaterialService;
import com.cleaningcloud.cld.service.ICldProjectRepositoryService;
import com.cleaningcloud.cld.service.mini.IMiniMaterialService;
import com.cleaningcloud.common.core.web.controller.BaseController;
import com.cleaningcloud.common.core.web.domain.AjaxResult;
import com.cleaningcloud.common.core.web.page.TableDataInfo;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 物资管理 - 小程序接口
 */
@RestController
@RequestMapping("/mini/material")
public class MiniMaterialController extends BaseController {

    @Autowired
    private ICldMaterialAppService materialAppService;

    @Autowired
    private IMiniMaterialService miniMaterialService;

    @Autowired
    private ICldMaterialAllocateService cldMaterialAllocateService;

    @Autowired
    private ICldProjectRepositoryService projectRepositoryService;

    @Autowired
    private ICldProjectMaterialService cldProjectMaterialService;

    @Operation(description = "查询项目物资阈值设置列表")
    @GetMapping("/getMaterialList")
    public TableDataInfo list(CldProjectMaterial cldProjectMaterial) {
        startPage();
//        cldProjectMaterial.getParams().put("isStock", 1);
        List<CldProjectMaterial> list = cldProjectMaterialService.selectCldProjectMaterialList(cldProjectMaterial);
        return getMiniDataTable(list);
    }


    @Operation(description = "物资申请记录")
    @GetMapping("/getMaterialAppList")
    public TableDataInfo getMaterialAppList(CldMaterialAppDto cldMaterialAppDto) {
        startPage();
        List<CldMaterialAppVo> list = materialAppService.selectCldMaterialAppList(cldMaterialAppDto);
        return getMiniDataTable(list);
    }

    @Operation(description = "申请项目物资")
    @PostMapping("/applyMaterial")
    public AjaxResult applyMaterial(@RequestBody CldMaterialAppDto cldMaterialApp) {
        return toAjax(materialAppService.insertCldMaterialApp(cldMaterialApp));
    }

    @Operation(description = "项目申请明细")
    @GetMapping("/getMaterialApp/{materialAppId}")
    public AjaxResult getMaterialApp(@PathVariable Long materialAppId) {
        return success(materialAppService.selectCldMaterialAppByMaterialAppId(materialAppId));
    }


    @Operation(description = "物资下发")
    @PostMapping("/materialsIssue")
    public AjaxResult materialsIssue(@RequestBody CldMaterialAllocateDto cldMaterialAllocateDto) {
        return toAjax(miniMaterialService.materialsIssue(cldMaterialAllocateDto));
    }

    @Operation(description = "物资下发撤销")
    @PostMapping("/materialsRevocation/{materialAllocateId}")
    public AjaxResult materialsRevocation(@PathVariable Long materialAllocateId) {
        return toAjax(miniMaterialService.materialsRevocation(materialAllocateId));
    }

    @Operation(description = "领用记录")
    @GetMapping("/getMaterialUseList")
    public TableDataInfo getMaterialApp(CldMaterialAllocateDto cldMaterialAppDto) {
        startPage();
        List<CldMaterialAllocateVo> list = cldMaterialAllocateService.selectCldUseList(cldMaterialAppDto);
        return getMiniDataTable(list);
    }

    @Operation(description = "领用记录明细")
    @GetMapping("/getMaterialUse/{materialAllocateId}")
    public AjaxResult getMaterialUse(@PathVariable Long materialAllocateId) {
        return success(cldMaterialAllocateService.selectCldMaterialAllocateByMaterialAllocateId(materialAllocateId));
    }

    @Operation(description = "确认领取")
    @PostMapping("/materialsConfirmCollection/{materialAllocateId}")
    public AjaxResult materialsConfirmCollection(@PathVariable Long materialAllocateId) {
        return toAjax(miniMaterialService.materialsConfirmCollection(materialAllocateId));
    }

    @Operation(description = "物资报损申请")
    @PostMapping("/materialsConsume")
    public AjaxResult materialsConsume(@RequestBody CldMaterialConsumeDto cldMaterialConsumeDto) {
        return toAjax(miniMaterialService.materialsConsume(cldMaterialConsumeDto));
    }

    @Operation(description = "我的物资查看")
    @GetMapping("/selectMySupplies/{projectPostId}")
    public TableDataInfo selectMySupplies(@PathVariable Long projectPostId) {
        startPage();
        return getMiniDataTable(miniMaterialService.selectMySupplies(projectPostId));
    }


    @Operation(description = "报损列表查看(我的报损/员工报损)")
    @GetMapping("/selectSuppliesConsumeList")
    public TableDataInfo selectSuppliesConsumeList(@RequestParam(name = "projectId", required = false) Long projectId,
                                                   @RequestParam(name = "consumeWareId", required = false) Long consumeWareId,
                                                   @RequestParam(name = "groupName", required = false) Long groupName,
                                                   @RequestParam(name = "dateMonth", required = false) String dateMonth,
                                                   @RequestParam(name = "pageSize", required = false) Integer pageSize,
                                                   @RequestParam(name = "pageNum", required = false) Integer pageNum) {
        Map<String, Object> map = new HashMap<>();
        map.put("projectId", projectId);
        map.put("consumeWareId", consumeWareId);
        map.put("consumeMonth", dateMonth);
        map.put("groupName", groupName);
        startPage();
        List<Map<String, Object>> list = miniMaterialService.selectSuppliesConsumeList(map);
        return getMiniDataTable(list);
    }


    @Operation(description = "按日查询消耗列表")
    @GetMapping("/selectDaySuppliesConsumeList")
    public TableDataInfo selectDaySuppliesConsumeList(@RequestParam(name = "projectId", required = false) Long projectId,
                                                      @RequestParam(name = "dateMonth", required = false) String dateMonth,
                                                      @RequestParam(name = "pageSize", required = false) Integer pageSize,
                                                      @RequestParam(name = "groupName", required = false) Long groupName,
                                                      @RequestParam(name = "pageNum", required = false) Integer pageNum) {
        Map<String, Object> map = new HashMap<>();
        map.put("projectId", projectId);
        map.put("consumeTime", dateMonth);
        map.put("groupName", groupName);
        startPage();
        List<Map<String, Object>> list = miniMaterialService.selectDaySuppliesConsumeList(map);
        return getMiniDataTable(list);
    }

    @Operation(description = "月消耗累计")
    @GetMapping("/getSuppliesConsumeMonthlyTotal")
    public AjaxResult getSuppliesConsumeMonthlyTotal(@RequestParam(name = "projectId") Long projectId,
                                                     @RequestParam(name = "dateMonth") String dateMonth,
                                                     @RequestParam(name = "groupName", required = false) Long groupName,
                                                     @RequestParam(name = "consumeWareId", required = false) Long consumeWareId) {
        Map<String, Object> map = new HashMap<>();
        map.put("projectId", projectId);
        map.put("consumeTime", dateMonth);
        map.put("groupName", groupName);
        map.put("consumeWareId", consumeWareId);
        return success(miniMaterialService.getSuppliesConsumeMonthlyTotal(map));
    }


    @Operation(description = "查询项目下库存")
    @GetMapping("/getDepotStockList")
    public TableDataInfo getDepotStockList(CldMaterialDetails cldMaterialDetails) {
        startPage();
        List<CldMaterialDetails> list = projectRepositoryService.selectCldMaterialDetailsList(cldMaterialDetails);
        return getMiniDataTable(list);
    }

    @Operation(description = "员工库存")
    @GetMapping("/getEmployeeInventory/{projectId}")
    public AjaxResult getEmployeeInventory(@PathVariable Long projectId) {
        return success(miniMaterialService.getEmployeeInventory(projectId));
    }

    @Operation(description = "根据商品ID查询员工库存")
    @GetMapping("/getEmployeeInventoryByCommodityId")
    public AjaxResult getEmployeeInventoryByCommodityId(@RequestParam(name = "projectId") Long projectId,
                                                        @RequestParam(name = "commodityId") Long commodityId) {
        Map<String, Object> map = new HashMap<>();
        map.put("projectId", projectId);
        map.put("commodityId", commodityId);
        return success(miniMaterialService.getEmployeeInventoryByCommodityId(map));
    }

}
